"""
写文件
"""

import os
import logging

__all__ = [
    "write_text",  # ->     写文本文件
    "write_bytes",  # ->    写二进制文件
]

logger = logging.getLogger()


def write_text(
        path: str,
        text: str,
        forced: bool = False,
) -> None:
    """
    写文本文件

    :param path:    路径
    :param text:    文本内容
    :param forced:  是否强制写入（是：强制写 / 否：已存在则不写入）
    :return:        None
    """

    try:
        if not forced and os.path.exists(path):
            logger.info(f"文本文件: {path} 已存在，无需写入")
            return

        with open(path, "w", encoding="UTF-8") as f:
            f.write(text)
            logger.info(f"成功写入文本文件: {path}")

    except Exception as e:
        logger.error(f"【写文本文件】错误：{e}")


def write_bytes(
        path: str,
        content: bytes,
        forced: bool = False,
) -> None:
    """
    写二进制文件

    :param path:    路径
    :param content: 二进制流
    :param forced:  是否强制写入（是：强制写 / 否：已存在则不写入）
    :return:        None
    """

    try:
        if not forced and os.path.exists(path):
            logger.info(f"二进制文件: {path} 已存在，无需写入")
            return

        with open(path, "wb") as f:
            f.write(content)
            logger.info(f"成功写入二进制文件: {path}")

    except Exception as e:
        logger.error(f"【写二进制文件】错误：{e}")
